﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Data.Entity;
using padnug.Domain;
using PadnugFramework;
using PadnugRepositoryInterfaces;

namespace PadnugRepository
{
    public class PadnugCalendarRepository : IPadnugCalendarRepository
    {
        private padnugEntities db; 
        public PadnugCalendarRepository()
        {
            db = new padnugEntities();
        }
        #region IPadnugCalendarRepository Members

        public IList<CalendarEvent> GetMonthsEvents(DateTime dateTime)
        {
            IList<CalendarEvent> events = new List<CalendarEvent>();
            var startDate = GetDateRangeStart(dateTime);
            var endDate = GetDateRangeEnd(dateTime);
            var dbMeetings = db.Meetings.Where(m => m.MeetingDate >= startDate && m.MeetingDate <= endDate).ToList();
            foreach (var meeting in dbMeetings)
            {
                var c = new CalendarEvent
                                      {
                                          CssClass = "Meeting",
                                          Description = "",
                                          EventID = meeting.ID,
                                          StartDateTime = meeting.MeetingDate.Value,
                                          Title = "x",
                                          URL = "/Events/Event/" + meeting.ID
                                      };
                events.Add(c);
            }
            return events;
        }

        private DateTime GetDateRangeStart(DateTime dateTime)
        {
            var startYear = dateTime.Year;
            var startMonth = dateTime.Month;
            var startDay = 1;
            return new DateTime(startYear, startMonth, startDay).AddDays(-7);
        }

        private DateTime GetDateRangeEnd(DateTime dateTime)
        {
            var endYear = dateTime.Year;
            var endMonth = dateTime.Month;
            var startDay = 1;
            return new DateTime(endYear, endMonth, startDay).AddMonths(1).AddDays(6);
        }

        public IList<CalendarEvent> GetAllEvents()
        {
            IList<CalendarEvent> events = new List<CalendarEvent>();
            var dbMeetings = db.Meetings.ToList();
            foreach (var meeting in dbMeetings)
            {
                var c = new CalendarEvent
                {
                    CssClass = "Meeting",
                    Description = "",
                    EventID = meeting.ID,
                    StartDateTime = meeting.MeetingDate.Value,
                    Title = "x",
                    URL = "/Events/Event/" + meeting.ID
                };
                events.Add(c);
            }
            return events;

        }

        public IList<DateTime> GetAllEventDates()
        {
            var dbDates = db.Meetings.Where(m=>m.MeetingDate.HasValue).OrderBy(m=>m.MeetingDate).Select(m => m.MeetingDate.Value).ToList();
            return dbDates;
        }

        #endregion
    }
}
